﻿@* --------------------------------------------------------------------------------------------------------------------
// <copyright file="SearchInputWidget.cshtml" company="Devbridge Group LLC">
// 
// Copyright (C) 2015,2016 Devbridge Group LLC
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/. 
// </copyright>
// 
// <summary>
// Better CMS is a publishing focused and developer friendly .NET open source CMS.
// 
// Website: https://www.bettercms.com 
// GitHub: https://github.com/devbridge/bettercms
// Email: info@bettercms.com
// </summary>
// -------------------------------------------------------------------------------------------------------------------- *@
@using Autofac
@using BetterCms.Module.Root.Mvc.Helpers
@using BetterCms.Module.Search
@using BetterCms.Module.Search.Content.Resources
@using BetterCms.Module.Search.Helpers
@using BetterCms.Module.Search.Services
@using BetterModules.Core.Dependencies

@model BetterCms.Module.Search.ViewModels.SearchInputViewModel

@{
    // Bind query from URL parameters
    Model.Query = Model.GetSearchQueryParameter(Request, Model.Query);
    
    string labelTitle = Model.GetOptionValue<string>(SearchModuleConstants.WidgetOptionNames.LabelTitle) ?? string.Empty;
    string submitTitle = Model.GetOptionValue<string>(SearchModuleConstants.WidgetOptionNames.SubmitTitle);
    string inputPlaceholder = Model.GetOptionValue<string>(SearchModuleConstants.WidgetOptionNames.InputPlaceholder);
    string searchResultsUrl = Model.GetOptionValue<string>(SearchModuleConstants.WidgetOptionNames.SearchResultsUrl);
    string queryParameterName = Model.GetOptionValue<string>(SearchModuleConstants.WidgetOptionNames.QueryParameterName);
    string formMethod = Model.GetOptionValue<string>(SearchModuleConstants.WidgetOptionNames.FormMethod);
    
    if (string.IsNullOrWhiteSpace(formMethod))
    {
        formMethod = SearchModuleConstants.WidgetOptionDefaultValues.FormMethod;
    }
    if (string.IsNullOrWhiteSpace(queryParameterName))
    {
        queryParameterName = SearchModuleConstants.WidgetOptionDefaultValues.QueryParameterName;
    }
    bool hasImplementation;
    using (var container = ContextScopeProvider.CreateChildContainer())
    {
        hasImplementation = container.IsRegistered<ISearchService>();
    }
}

@if (!hasImplementation)
{
    <h3 style="color: red;">@SearchGlobalization.Search_Module_Has_No_Service_Implementations</h3>
}
else 
{

    <form action="@searchResultsUrl" class="bcms-search-input-form" method="@formMethod">
        @Html.LabelFor(f => f.Query, labelTitle, new { @for = queryParameterName, @class = "bcms-search-input-label" })
        @Html.TextBoxFor(m => m.Query, new
                                           {
                                               @class = "bcms-search-input required", 
                                               @placeholder = inputPlaceholder, @Name = queryParameterName,
                                               maxlength = SearchModuleConstants.SearchQueryMaximumLength
                                           })
        @Html.ValidationMessageFor(m => m.Query)
        <button type="submit" class="bcms-search-submit">@submitTitle</button>    
    </form>
}